package com.hcb.logsdk.manager.schedulerTask;

import android.content.Context;
import android.util.Log;

import com.hcb.logsdk.manager.LOGSDKManager;
import com.hcb.logsdk.socketio.listener.IConstants;
import com.hcb.logsdk.socketio.socket.AppSocket;
import com.hcb.logsdk.util.BroadcastUtil;
import com.hcb.logsdk.util.C;
import com.hcb.logsdk.util.CheckUtil;
import com.hcb.logsdk.util.dodo.NetStatus;

import static com.hcb.logsdk.util.L.isConnected;

/**
 * @author WangGuoWei
 * @time 2018/3/1 10:48
 * @des ${TODO}
 * <p>
 * ┽
 * ┽                            _ooOoo_
 * ┽                           o8888888o
 * ┽                           88" . "88
 * ┽                           (| -_- |)
 * ┽                           O\  =  /O
 * ┽                        ____/`---'\____
 * ┽                      .'  \\|     |//  `.
 * ┽                     /  \\|||  :  |||//  \
 * ┽                    /  _||||| -:- |||||-  \
 * ┽                    |   | \\\  -  /// |   |
 * ┽                    | \_|  ''\---/''  |   |
 * ┽                    \  .-\__  `-`  ___/-. /
 * ┽                  ___`. .'  /--.--\  `. . __
 * ┽               ."" '<  `.___\_<|>_/___.'  >'"".
 * ┽              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 * ┽              \  \ `-.   \_ __\ /__ _/   .-` /  /
 * ┽         ======`-.____`-.___\_____/___.-`____.-'======
 * ┽                            `=---='
 * ┽         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 * ┽                      佛祖保佑       永无BUG
 * ┽
 * ┽
 * ┽
 * @updateAuthor $Author$
 * @updateDate $Date$
 * @updateDes ${TODO}
 */
public class CheckSocketConnectScheduledExecutor implements Runnable {

    private final Context ctx;

    public CheckSocketConnectScheduledExecutor(Context ctx){
        this.ctx = ctx;
    }

    @Override
    public void run() {
        Log.i("PushLogService", "开始执行Socket连接监控任务-----  "+ C.CUR_SOCKET_URL
                +"\n  ------isConnected   "+ AppSocket.getInstance().isConnected()
                +"   L.isConnected  "+ isConnected+"   ConnectCount:  "+C.SOCKET_CONNECT_COUNT);

        if(isConnected){
            BroadcastUtil.sendBroadcastToUI(ctx, IConstants.LOG_EVENT_CONNECT,"Socket连接成功!!!");
            Log.i("PushLogService", "Socket已经连接！！！--------------");
            LOGSDKManager.getInstance().sendLog(IConstants.SOCTET_EVENT_CLIENT_LOG_MESSAGE,
                    "========================= Socket已经连接！！！===============================");
            return;
        }


        if(C.SOCKET_CONNECT_COUNT>2){
            Log.i("PushLogService", "Socket重连次数-------ConnectCount: "+C.SOCKET_CONNECT_COUNT
                    +"  -----isServiceWorked   "+ CheckUtil.isServiceWorked(ctx,"com.hcb.logsdk.service.PushService"));
            if(CheckUtil.isServiceWorked(ctx,"com.hcb.logsdk.service.PushService")){
                if(NetStatus.getNetStatus(ctx)){
                    C.SOCKET_RECONNECT = true;
                    LOGSDKManager.getInstance().destroy();
                    Log.i("PushService", "Socket重连失败次数过多...重启服务...");
                }
            }

            return;
        }

        C.SOCKET_CONNECT_COUNT++;
        Log.i("PushLogService", "Socket已经断开！！！开始重连....--------------ConnectCount: "+C.SOCKET_CONNECT_COUNT);
        LOGSDKManager.getInstance().sendLog(IConstants.SOCTET_EVENT_CLIENT_LOG_MESSAGE,
                "================================= Socket已经断开！！！开始重连 ================================");
        LOGSDKManager.getInstance().startconnect();

    }


}
